import java.io.IOException;
import java.util.HashMap;

import data.cad.CAD;
import data.result.ScanResult;
import data.xml.XML;
import core.SemanticMatch;
import org.opencv.core.Core;

public class Main {

    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);//加载库,否则会出错
    }

    public static void main(String[] args)
    {
        var testArgs = new String[]
                {
                        "-d", "TestFile/drawinfo.json",
                        "-x", "TestFile/roomcontour_step1.xml",
                        "-s", "plaster", "-a", "1f1b6539-618d-4d12-8432-a399bc1e6d4a01",
                        "-m", "TestFile/result.json","-is","4","-im","95f66806-2d9f-4904-a74f-66eb1459ef6c"
                };
        var paramPairs = ParseParam(testArgs);
        match(paramPairs);

    }


    public static void match(HashMap<String, String> paramPairs)  {
        var cad = new CAD(paramPairs.get("-d"), paramPairs.get("-s"), paramPairs.get("-a"));
        var xml = new XML(paramPairs.get("-x"));
        var preMatchPath = paramPairs.getOrDefault("-pm", null);
        var cadId = paramPairs.get("-im");
        var xmlId = paramPairs.get("-is");
        var result = ScanResult.GetScanResult(paramPairs.get("-m"));
        var match = preMatchPath == null ? new SemanticMatch(cad, xml, cadId, xmlId) : new SemanticMatch(cad, xml, preMatchPath);
        if(xml.subWalls.isEmpty())
            xml.GenerateSubWallXML(cad, match, 0d, match.sameDirection);
        result.CompleteFields(match, cad, xml);
        result.Save(paramPairs.get("-m"));

    }

    /**
     * <summary>
     * parse command line parameters
     * ('--drawing', '-d', help='drawing data json file path')
     * ('--measurement', '-m', help='measurement result data json file path')
     * ('--old_measurement', '-om', help='old measurement result data json file path')
     * ('--xml', '-x', help='xml file path')
     * ('--auto', '-a', help='auto plan point id')
     * ('--id_in_scan', '-is', help='best for match in xml id')
     * ('--id_in_cad', '-ic', help='best for match in drawing id')
     * ('--rescan_old', '-r', help='old ids of rescan')
     * ('--feature', '-f', help='features of walls')
     * ('--pre_match', '-pm', help='supposed to be match.json, the pre-match result')
     * ('--coord', '-c', station 2d coordinate in pdf.eg: (10000,20000) or [10000,20000]')
     * ('--stage', '-s', help='construction stage station in pdf. enum: concrete,plaster,masonry')
     * ('--id_for_manual', '-im', help='id for manually planning station ')
     * ('--tolerance', '-t', help='match result credibility is 1, when tolerance is not None')
     * </summary>
     * <param name="args"></param>
     * <returns></returns>
     */
    private static HashMap<String, String> ParseParam(String[] args) {
        HashMap<String, String> paramPairs = new HashMap<String, String>();
        for (int i = 0; i < args.length; i++) {
            if (args[i].startsWith("-"))
                paramPairs.put(args[i], args[i + 1]);
        }

        return paramPairs;
    }
}